package code.part05_IO;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @author: ZJT
 * @Time: 2021/7/20
 * @Description: 流
 */
public class P02_Stream {

    //字节流

    /**
     * @author: ZJT
     * @Time: 2021/7/20
     * @Description: 输入流
     */
    @Test
    public void inputStream() {

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("src/code/part05_IO/abc.txt");
            int read = inputStream.read();
            System.out.println((char) read);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @author: ZJT
     * @Time: 2021/7/20
     * @Description: 输出流
     */
    @Test
    public void outputStream() {

        File file = new File("src/code/part05_IO/abc.txt");
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            outputStream.write(99);
            outputStream.write("\r\nabc".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @author: ZJT
     * @Time: 2021/7/20
     * @Description: 读
     */
    @Test
    public void read() {

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("src/code/part05_IO/abc.txt");
            int read = 0;
            //循环输出所有的字节，
            while ((read = inputStream.read()) != -1) {
                System.out.println((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @author: ZJT
     * @Time: 2021/7/20
     * @Description: 缓冲读
     */
    @Test
    public void readLen() {

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("src/code/part05_IO/abc.txt");
            int length = 0;
            //添加缓冲区的方式进行读取，每次会将数据添加到缓冲区中，当缓冲区满了之后，一次 读取，而不是每一个字节进行读取
            byte[] buffer = new byte[1024];
            while ((length = inputStream.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @author: ZJT
     * @Time: 2021/7/20
     * @Description: 缓冲读
     */
    @Test
    public void readLenOffset() {

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("src/code/part05_IO/abc.txt");
            int length = 0;
            byte[] buffer = new byte[1024];
            //off - 在其处写入数据的数组的初始偏移量
            //len - 要读取的最大字节数
            while ((length = inputStream.read(buffer, 5, 10)) != -1) {
                System.out.println(new String(buffer, 5, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //字符流

    /**
     * @author: ZJT
     * @Time: 2021/7/20
     * @Description: 字符流读
     * @param:
     * @return:
     * @throws:
     */
    @Test
    public void fileReader() {
        Reader reader = null;
        try {
            reader = new FileReader("src/code/part05_IO/abc.txt");
            int length = 0;
            char[] chars = new char[1024];
            //添加缓冲区
            while ((length = reader.read(chars)) != -1) {
                System.out.println(new String(chars, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @author: ZJT
     * @Time: 2021/7/20
     * @Description: 字符流写
     * @param:
     * @return:
     * @throws:
     */
    @Test
    public void fileWriter() {
        File file = new File("writer.txt");
        Writer writer = null;
        try {
            writer = new FileWriter(file);
            writer.write("www");
            writer.write("中文");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @author: ZJT
     * @Time: 2021/7/21
     * @Description: 从字符流到字节流的桥接
     * @param:
     * @return: 
     * @throws: 
     */
    @Test
    public void fileOutputStream() {

        File file = new File("src/code/part05_IO/abc.txt");
        OutputStreamWriter outputStreamWriter = null;
        FileOutputStream fileOutputStream = null;


        try {
            fileOutputStream = new FileOutputStream(file);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
            outputStreamWriter.write("中文");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流对象的时候，建议按照创建的顺序的逆序来进行关闭
            try {
                outputStreamWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

}
